home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / multiprocessing / synchronize.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  10KB  |  302 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'Lock',
  6.     'RLock',
  7.     'Semaphore',
  8.     'BoundedSemaphore',
  9.     'Condition',
  10.     'Event']
  11. import threading
  12. import os
  13. import sys
  14. from time import time as _time, sleep as _sleep
  15. import _multiprocessing
  16. from multiprocessing.process import current_process
  17. from multiprocessing.util import Finalize, register_after_fork, debug
  18. from multiprocessing.forking import assert_spawning, Popen
  19.  
  20. try:
  21.     from _multiprocessing import SemLock
  22. except ImportError:
  23.     raise ImportError('This platform lacks a functioning sem_open' + ' implementation, therefore, the required' + ' synchronization primitives needed will not' + ' function, see issue 3770.')
  24.  
  25. (RECURSIVE_MUTEX, SEMAPHORE) = range(2)
  26. SEM_VALUE_MAX = _multiprocessing.SemLock.SEM_VALUE_MAX
  27.  
  28. class SemLock(object):
  29.     
  30.     def __init__(self, kind, value, maxvalue):
  31.         sl = self._semlock = _multiprocessing.SemLock(kind, value, maxvalue)
  32.         debug('created semlock with handle %s' % sl.handle)
  33.         self._make_methods()
  34.         if sys.platform != 'win32':
  35.             
  36.             def _after_fork(obj):
  37.                 obj._semlock._after_fork()
  38.  
  39.             register_after_fork(self, _after_fork)
  40.         
  41.  
  42.     
  43.     def _make_methods(self):
  44.         self.acquire = self._semlock.acquire
  45.         self.release = self._semlock.release
  46.         self.__enter__ = self._semlock.__enter__
  47.         self.__exit__ = self._semlock.__exit__
  48.  
  49.     
  50.     def __getstate__(self):
  51.         assert_spawning(self)
  52.         sl = self._semlock
  53.         return (Popen.duplicate_for_child(sl.handle), sl.kind, sl.maxvalue)
  54.  
  55.     
  56.     def __setstate__(self, state):
  57.         self._semlock = _multiprocessing.SemLock._rebuild(*state)
  58.         debug('recreated blocker with handle %r' % state[0])
  59.         self._make_methods()
  60.  
  61.  
  62.  
  63. class Semaphore(SemLock):
  64.     
  65.     def __init__(self, value = 1):
  66.         SemLock.__init__(self, SEMAPHORE, value, SEM_VALUE_MAX)
  67.  
  68.     
  69.     def get_value(self):
  70.         return self._semlock._get_value()
  71.  
  72.     
  73.     def __repr__(self):
  74.         
  75.         try:
  76.             value = self._semlock._get_value()
  77.         except Exception:
  78.             value = 'unknown'
  79.  
  80.         return '<Semaphore(value=%s)>' % value
  81.  
  82.  
  83.  
  84. class BoundedSemaphore(Semaphore):
  85.     
  86.     def __init__(self, value = 1):
  87.         SemLock.__init__(self, SEMAPHORE, value, value)
  88.  
  89.     
  90.     def __repr__(self):
  91.         
  92.         try:
  93.             value = self._semlock._get_value()
  94.         except Exception:
  95.             value = 'unknown'
  96.  
  97.         return '<BoundedSemaphore(value=%s, maxvalue=%s)>' % (value, self._semlock.maxvalue)
  98.  
  99.  
  100.  
  101. class Lock(SemLock):
  102.     
  103.     def __init__(self):
  104.         SemLock.__init__(self, SEMAPHORE, 1, 1)
  105.  
  106.     
  107.     def __repr__(self):
  108.         
  109.         try:
  110.             if self._semlock._is_mine():
  111.                 name = current_process().name
  112.                 if threading.current_thread().name != 'MainThread':
  113.                     name += '|' + threading.current_thread().name
  114.                 
  115.             elif self._semlock._get_value() == 1:
  116.                 name = 'None'
  117.             elif self._semlock._count() > 0:
  118.                 name = 'SomeOtherThread'
  119.             else:
  120.                 name = 'SomeOtherProcess'
  121.         except Exception:
  122.             name = 'unknown'
  123.  
  124.         return '<Lock(owner=%s)>' % name
  125.  
  126.  
  127.  
  128. class RLock(SemLock):
  129.     
  130.     def __init__(self):
  131.         SemLock.__init__(self, RECURSIVE_MUTEX, 1, 1)
  132.  
  133.     
  134.     def __repr__(self):
  135.         
  136.         try:
  137.             if self._semlock._is_mine():
  138.                 name = current_process().name
  139.                 if threading.current_thread().name != 'MainThread':
  140.                     name += '|' + threading.current_thread().name
  141.                 
  142.                 count = self._semlock._count()
  143.             elif self._semlock._get_value() == 1:
  144.                 (name, count) = ('None', 0)
  145.             elif self._semlock._count() > 0:
  146.                 (name, count) = ('SomeOtherThread', 'nonzero')
  147.             else:
  148.                 (name, count) = ('SomeOtherProcess', 'nonzero')
  149.         except Exception:
  150.             (name, count) = ('unknown', 'unknown')
  151.  
  152.         return '<RLock(%s, %s)>' % (name, count)
  153.  
  154.  
  155.  
  156. class Condition(object):
  157.     
  158.     def __init__(self, lock = None):
  159.         if not lock:
  160.             pass
  161.         self._lock = RLock()
  162.         self._sleeping_count = Semaphore(0)
  163.         self._woken_count = Semaphore(0)
  164.         self._wait_semaphore = Semaphore(0)
  165.         self._make_methods()
  166.  
  167.     
  168.     def __getstate__(self):
  169.         assert_spawning(self)
  170.         return (self._lock, self._sleeping_count, self._woken_count, self._wait_semaphore)
  171.  
  172.     
  173.     def __setstate__(self, state):
  174.         (self._lock, self._sleeping_count, self._woken_count, self._wait_semaphore) = state
  175.         self._make_methods()
  176.  
  177.     
  178.     def _make_methods(self):
  179.         self.acquire = self._lock.acquire
  180.         self.release = self._lock.release
  181.         self.__enter__ = self._lock.__enter__
  182.         self.__exit__ = self._lock.__exit__
  183.  
  184.     
  185.     def __repr__(self):
  186.         
  187.         try:
  188.             num_waiters = self._sleeping_count._semlock._get_value() - self._woken_count._semlock._get_value()
  189.         except Exception:
  190.             num_waiters = 'unkown'
  191.  
  192.         return '<Condition(%s, %s)>' % (self._lock, num_waiters)
  193.  
  194.     
  195.     def wait(self, timeout = None):
  196.         if not self._lock._semlock._is_mine():
  197.             raise AssertionError, 'must acquire() condition before using wait()'
  198.         self._sleeping_count.release()
  199.         count = self._lock._semlock._count()
  200.         for i in xrange(count):
  201.             self._lock.release()
  202.         
  203.         
  204.         try:
  205.             self._wait_semaphore.acquire(True, timeout)
  206.         finally:
  207.             self._woken_count.release()
  208.             for i in xrange(count):
  209.                 self._lock.acquire()
  210.             
  211.  
  212.  
  213.     
  214.     def notify(self):
  215.         if not self._lock._semlock._is_mine():
  216.             raise AssertionError, 'lock is not owned'
  217.         if not not self._wait_semaphore.acquire(False):
  218.             raise AssertionError
  219.         while self._woken_count.acquire(False):
  220.             res = self._sleeping_count.acquire(False)
  221.             if not res:
  222.                 raise AssertionError
  223.             continue
  224.             res
  225.  
  226.     
  227.     def notify_all(self):
  228.         if not self._lock._semlock._is_mine():
  229.             raise AssertionError, 'lock is not owned'
  230.         if not not self._wait_semaphore.acquire(False):
  231.             raise AssertionError
  232.         while self._woken_count.acquire(False):
  233.             res = self._sleeping_count.acquire(False)
  234.             if not res:
  235.                 raise AssertionError
  236.             continue
  237.             res
  238.         sleepers = 0
  239.         while self._sleeping_count.acquire(False):
  240.             self._wait_semaphore.release()
  241.             sleepers += 1
  242.             continue
  243.             not self._wait_semaphore.acquire(False)
  244.  
  245.  
  246.  
  247. class Event(object):
  248.     
  249.     def __init__(self):
  250.         self._cond = Condition(Lock())
  251.         self._flag = Semaphore(0)
  252.  
  253.     
  254.     def is_set(self):
  255.         self._cond.acquire()
  256.         
  257.         try:
  258.             if self._flag.acquire(False):
  259.                 self._flag.release()
  260.                 return True
  261.             return False
  262.         finally:
  263.             self._cond.release()
  264.  
  265.  
  266.     
  267.     def set(self):
  268.         self._cond.acquire()
  269.         
  270.         try:
  271.             self._flag.acquire(False)
  272.             self._flag.release()
  273.             self._cond.notify_all()
  274.         finally:
  275.             self._cond.release()
  276.  
  277.  
  278.     
  279.     def clear(self):
  280.         self._cond.acquire()
  281.         
  282.         try:
  283.             self._flag.acquire(False)
  284.         finally:
  285.             self._cond.release()
  286.  
  287.  
  288.     
  289.     def wait(self, timeout = None):
  290.         self._cond.acquire()
  291.         
  292.         try:
  293.             if self._flag.acquire(False):
  294.                 self._flag.release()
  295.             else:
  296.                 self._cond.wait(timeout)
  297.         finally:
  298.             self._cond.release()
  299.  
  300.  
  301.  
  302.